<template>
  <el-dialog :title="`${title}用户`" :visible.sync="dialogVisible" width="35%" :before-close="handleClose">
    <el-form :model="form" :rules="rules" ref="ruleForm">
      <el-form-item label="头像：" label-width="100px" prop="name">
        <el-image
          :src="form.avatar ? $baseUrl + form.avatar : 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'"
          style="width: 100px; height: 100px"
          :preview-src-list="[form.avatar ? $baseUrl + form.avatar : 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png']"
        ></el-image>
      </el-form-item>
      <el-form-item label="用户名：" label-width="100px" prop="user_name">
        <el-input v-model="form.user_name" style="width: 50%" clearable disabled></el-input>
      </el-form-item>
      <el-form-item label="密码：" label-width="100px" prop="password">
        <el-input v-model="form.password" style="width: 50%" show-password></el-input>
      </el-form-item>
      <el-form-item label="角色：" label-width="100px" prop="roles">
        <el-select v-model="form.roles" placeholder="请选择角色" style="width: 50%">
          <el-option v-for="item in role_list" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="注册时间：" label-width="100px" prop="status">
        {{ form.create_time }}
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="confirm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import NewUpload from '@/components/NewUpload'
  import { editUesr } from '@/api/user'
  export default {
    components: { NewUpload },
    data() {
      return {
        title: '编辑',
        dialogVisible: false,
        form: {},
        disabledRoles: false,
        role_list: [
          { value: 'admin', label: '超级管理员' },
          { value: 'user', label: '普通商家' },
        ],
        value: [''],
        rules: {
          user_name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
          password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
          roles: [{ required: true, message: '请选择角色', trigger: 'blur' }],
        },
      }
    },
    watch: {},
    methods: {
      confirm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('确认编辑该用户?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            }).then(async () => {
              await editUesr(this.form).then((data) => {
                if (data.code == 200) {
                  this.$message({
                    message: '修改成功',
                    type: 'success',
                  })
                  this.dialogVisible = false
                  this.$emit('getList')
                } else {
                  this.$message({
                    message: '修改失败',
                    type: 'error',
                  })
                }
              })
            })
          } else {
            return false
          }
        })
      },
      open(row) {
        console.log('row: ', row)
        if (row.avatar) {
          row.avatar = [row.avatar]
        }
        this.$nextTick(() => {
          this.form = row
        })
        this.dialogVisible = true
      },

      handleClose() {
        this.dialogVisible = false
      },
    },
  }
</script>

<style></style>
